//
//  XRmenuBoxBase.m
//  XRmenu
//
//  Created by Gao Yan on 11-11-23.
//  Copyright 2011年 foxhis. All rights reserved.
//

#import "XRmenuBoxBase.h"
#define kTagOfBoxImage 501

@implementation XRmenuBoxBase
-(id) initWithBoxStyleName:(NSString*)stylename Frame:(CGRect)fr
{
	self = [super initWithFrame:fr];
	if (self) {
		[self setBackgroundColor:[UIColor clearColor]];
		NSDictionary *bdict;
		if ([[[APPSESSION.allconfig objectForKey:@"UIKit CSS"] objectForKey:@"Box CSS"] objectForKey:stylename]) {
			bdict = [[[APPSESSION.allconfig objectForKey:@"UIKit CSS"] objectForKey:@"Box CSS"] objectForKey:stylename];
		}else {
			bdict = [[[APPSESSION.allconfig objectForKey:@"UIKit CSS"] objectForKey:@"Box CSS"] objectForKey:@"default box"];
		}
		[self drawiMenu2Box:fr Parm:bdict];
	}
	return self;
}

-(void) addTableViewWithStyleName:(NSString*)stylename Frame:(CGRect)fr
{
	NSDictionary *tdict = [APPSESSION.allconfig findAtPath:[NSString stringWithFormat:@"UIKit CSS/TableView CSS/%@", stylename]];
	[self setTableViewProperty:tdict Frame:fr];
}

-(void) drawiMenu2Box:(CGRect)fr Parm:(NSDictionary*)bdict
{
	NSString *tmpfname;
	UIImageView *tmpIv;
	UIImage *tmpImg;
	CGRect cr;
	float w, h;
	float w1, h1;
	float w2, h2;
	
	tmpfname = [XRmenuPublic getGeneralSkinImg:[bdict objectForKey:@"lefttop"]];
	tmpImg = [[UIImage alloc] initWithContentsOfFile:tmpfname];
	w = tmpImg.size.width; h = tmpImg.size.height;
	cr = CGRectMake(0, 0, w, h);
	tmpIv = [[UIImageView alloc] initWithFrame:cr];
	[tmpIv setImage:tmpImg];
	tmpIv.tag = kTagOfBoxImage;
	[self addSubview:tmpIv];
	[tmpImg release];
	[tmpIv release];
    
	tmpfname = [XRmenuPublic getGeneralSkinImg:[bdict objectForKey:@"righttop"]];
	tmpImg = [[UIImage alloc] initWithContentsOfFile:tmpfname];
	w2 = tmpImg.size.width; h2 = tmpImg.size.height;
	cr = CGRectMake(fr.size.width - w2, 0, w2, h);
	tmpIv = [[UIImageView alloc] initWithFrame:cr];
	[tmpIv setImage:tmpImg];
	tmpIv.tag = kTagOfBoxImage;
	[self addSubview:tmpIv];
	[tmpImg release];
	[tmpIv release];
    
	tmpfname = [XRmenuPublic getGeneralSkinImg:[bdict objectForKey:@"top"]];
	tmpImg = [[UIImage alloc] initWithContentsOfFile:tmpfname];
	cr = CGRectMake(w, 0, fr.size.width - w - w2, h);
	tmpIv = [[UIImageView alloc] initWithFrame:cr];
	[tmpIv setImage:tmpImg];
	tmpIv.tag = kTagOfBoxImage;
	[self addSubview:tmpIv];
	[tmpImg release];
	[tmpIv release];
	
	tmpfname = [XRmenuPublic getGeneralSkinImg:[bdict objectForKey:@"leftbottom"]];
	tmpImg = [[UIImage alloc] initWithContentsOfFile:tmpfname];
	w1 = tmpImg.size.width; h1 = tmpImg.size.height;
	cr = CGRectMake(0, fr.size.height - h1, w1, h1);
	tmpIv = [[UIImageView alloc] initWithFrame:cr];
	[tmpIv setImage:tmpImg];
	tmpIv.tag = kTagOfBoxImage;
	[self addSubview:tmpIv];
	[tmpImg release];
	[tmpIv release];
    
	tmpfname = [XRmenuPublic getGeneralSkinImg:[bdict objectForKey:@"bottom"]];
	tmpImg = [[UIImage alloc] initWithContentsOfFile:tmpfname];
	cr = CGRectMake(w1, fr.size.height - h1, fr.size.width - w1 - w2, h1);
	tmpIv = [[UIImageView alloc] initWithFrame:cr];
	[tmpIv setImage:tmpImg];
	tmpIv.tag = kTagOfBoxImage;
	[self addSubview:tmpIv];
	[tmpImg release];
	[tmpIv release];
	
	tmpfname = [XRmenuPublic getGeneralSkinImg:[bdict objectForKey:@"rightbottom"]];
	tmpImg = [[UIImage alloc] initWithContentsOfFile:tmpfname];
	w2 = tmpImg.size.width; h2 = tmpImg.size.height;
	cr = CGRectMake(fr.size.width - w2, fr.size.height - h2, w2, h2);
	tmpIv = [[UIImageView alloc] initWithFrame:cr];
	[tmpIv setImage:tmpImg];
	tmpIv.tag = kTagOfBoxImage;
	[self addSubview:tmpIv];
	[tmpImg release];
	[tmpIv release];
    
	tmpfname = [XRmenuPublic getGeneralSkinImg:[bdict objectForKey:@"left"]];
	tmpImg = [[UIImage alloc] initWithContentsOfFile:tmpfname];
	cr = CGRectMake(0, h, w, fr.size.height - h - h1);
	tmpIv = [[UIImageView alloc] initWithFrame:cr];
	[tmpIv setImage:tmpImg];
	tmpIv.tag = kTagOfBoxImage;
	[self addSubview:tmpIv];
	[tmpImg release];
	[tmpIv release];
    
	tmpfname = [XRmenuPublic getGeneralSkinImg:[bdict objectForKey:@"right"]];
	tmpImg = [[UIImage alloc] initWithContentsOfFile:tmpfname];
	w2 = tmpImg.size.width; h2 = tmpImg.size.height;
	cr = CGRectMake(fr.size.width - w2 , h, w2, fr.size.height - h - h1);
	tmpIv = [[UIImageView alloc] initWithFrame:cr];
	[tmpIv setImage:tmpImg];
	tmpIv.tag = kTagOfBoxImage;
	[self addSubview:tmpIv];
	[tmpImg release];
	[tmpIv release];
}

#pragma mark 自动定位的
-(id) initWithAutoBox:(NSString*)boxstyle ParentFrame:(CGRect)pfr BoxSize:(CGSize)bsize NSPriority:(BOOL)isNS
{
	
	pfr = CGRectMake(pfr.origin.x, pfr.origin.y-51, pfr.size.width, pfr.size.height);
    NSDictionary *bdict = [APPSESSION.allconfig findAtPath:[NSString stringWithFormat:@"UIKit CSS/Box CSS/%@", boxstyle]];
    if (bdict) {
        NSInteger aposition = [self judgePosition:pfr BoxSize:bsize NSPriority:isNS];
		CGRect brect = [self caculateBoxRect:pfr BSize:bsize APosition:aposition];
		self = [super initWithFrame:brect];
		if (self) {
			[self drawiMenu2Box:brect Parm:bdict];
			[self drawiMenu2BoxArrow:brect Parm:bdict APosition:aposition];
		}
    }else{
        NSAssert(NO, @"系统配置参数中Box类型定义错误");
    }
    return self;
}

-(void) drawiMenu2BoxArrow:(CGRect)fr Parm:(NSDictionary*)bdict APosition:(NSInteger)ap 
{
	NSString *tmpfname;
	UIImageView *tmpIv;
	UIImage *tmpImg;
	CGRect cr;
	float w, h;
	CGFloat angle;
	
	tmpfname = [XRmenuPublic getGeneralSkinImg:[bdict objectForKey:@"arrow"]];
	tmpImg = [[UIImage alloc] initWithContentsOfFile:tmpfname];
	w = tmpImg.size.width; h = tmpImg.size.height;
	switch (ap) {
		case 1:
			cr = CGRectMake(0, 0, w, h);
			angle = 0;
			break;
		case 2:
			cr = CGRectMake(fr.size.width - w, 0, w, h);
			angle = 0;
			break;
		case 3:
			cr = CGRectMake(fr.size.width - w/2, -h/2, w, h);
			angle = M_PI_2;
			break;
		case 4:
			cr = CGRectMake(fr.size.width - w/2, fr.size.height - w - h/2, w, h);
			angle = M_PI_2;
			break;
		case 5:
			cr = CGRectMake(fr.size.width - w/2, fr.size.height - h/2, w, h);
			angle = M_PI;
			break;
		case 6:
			cr = CGRectMake(w/2, fr.size.height-h/2, w, h);
			angle = M_PI;
			break;
		case 7:
			cr = CGRectMake(-w/2, fr.size.height-h/2, w, h);
			angle = -M_PI_2;
			break;
		case 8:
			cr = CGRectMake(-w/2, w-h/2, w, h);
			angle = -M_PI_2;
			break;
		default:
			break;
	}
	tmpIv = [[UIImageView alloc] initWithFrame:cr];
	[tmpIv setImage:tmpImg];
	if (angle != 0) {
		tmpIv.layer.anchorPoint = CGPointMake(0, 0);
		tmpIv.layer.transform = CATransform3DRotate(tmpIv.layer.transform, angle, 0, 0, 1);
	}
	tmpIv.tag = kTagOfBoxImage;
	[self addSubview:tmpIv];
	[tmpImg release];
	[tmpIv release];
	
}

-(CGRect) caculateBoxRect:(CGRect)pfr BSize:(CGSize)bsize APosition:(NSInteger)ap 
{
	CGRect ret;
	
	switch (ap) {
		case 1:
			ret = CGRectMake(pfr.origin.x, pfr.origin.y + pfr.size.height, bsize.width, bsize.height);
			break;
		case 2:
			ret = CGRectMake(pfr.origin.x + pfr.size.width - bsize.width, pfr.origin.y + pfr.size.height, bsize.width, bsize.height);
			break;
		case 3:
			ret = CGRectMake(pfr.origin.x - bsize.width, pfr.origin.y, bsize.width, bsize.height);
			break;
		case 4:
			ret = CGRectMake(pfr.origin.x - bsize.width, pfr.origin.y + pfr.size.height - bsize.height, bsize.width, bsize.height);
			break;
		case 5:
			ret = CGRectMake(pfr.origin.x + pfr.size.width - bsize.width, pfr.origin.y - bsize.height, bsize.width, bsize.height);
			break;
		case 6:
			ret = CGRectMake(pfr.origin.x, pfr.origin.y - bsize.height, bsize.width, bsize.height);
			break;
		case 7:
			ret = CGRectMake(pfr.origin.x + pfr.size.width, pfr.origin.y + pfr.size.height - bsize.height, bsize.width, bsize.height);
			break;
		case 8:
			ret = CGRectMake(pfr.origin.x + pfr.size.width, pfr.origin.y, bsize.width, bsize.height);
			break;
		default:
			ret = CGRectZero;
			break;
	}
	return ret;
}

-(NSInteger) judgePosition:(CGRect)pfr BoxSize:(CGSize)bsize NSPriority:(BOOL)isNS
{
    // Box左上角箭头位置从1开始顺时针每边2个
    NSInteger xlimit, ylimit;
    if ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortrait) {
        xlimit = 768; ylimit = 1024;
    }else{
        xlimit = 1024; ylimit = 768;
    }
    
    if (isNS) {
        if (((pfr.origin.x + bsize.width) < xlimit) && ((pfr.origin.y + pfr.size.height + bsize.height) < ylimit)) {
            return 1;
        }
        if (((pfr.origin.x + pfr.size.width - bsize.width) > 0) && ((pfr.origin.y + pfr.size.height + bsize.height) < ylimit)) {
            return 2;
        }
        if (((pfr.origin.x + bsize.width) < xlimit) && ((pfr.origin.y - bsize.height) > 0)) {
            return 6;
        }
        if (((pfr.origin.x + pfr.size.width - bsize.width) > 0) && ((pfr.origin.y - bsize.height) > 0)) {
            return 5;
        }
    }else{
        if (((pfr.origin.x + pfr.size.width + bsize.width) < xlimit) && ((pfr.origin.y + bsize.height) < ylimit)) {
            return 8;
        }
        if (((pfr.origin.x + pfr.size.width + bsize.width) < xlimit) && ((pfr.origin.y + pfr.size.height - bsize.height) > 0)) {
            return 7;
        }
        if (((pfr.origin.x - bsize.width) > 0) && ((pfr.origin.y + bsize.height) < ylimit)) {
            return 3;
        }
        if (((pfr.origin.x - bsize.width) < 0) && ((pfr.origin.y + pfr.size.height - bsize.height) > 0)) {
            return 4;
        }
    }
	NSAssert3(NO, @"无法使用参数PFrame:%@, Size:%@绘制标准box(%@)", NSStringFromCGRect(pfr), NSStringFromCGSize(bsize), (isNS?@"NS":@"EW"));
	return 0;
}
@end